package com.ddzj.scaffold.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.collections.RedisProperties;
import org.springframework.stereotype.Component;

/**
 * @Author:yangzibo
 * @CreateTime:2024-04-14 23:15
 * @Description:
 * @Version:1.0
 **/
@Configuration
//@Configuration(proxyBeanMethods = false)
//@ConditionalOnClass(RedisOperations.class)
public class RedisConfig{
    //https://blog.csdn.net/m0_47044985/article/details/131183561

    /*@Bean("RedisConnectionFactory")
    public RedisConnectionFactory getJedisConnectionFactory() {
        //集群
        // RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration();
        //单机redis
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("localhost", 6379);
        config.setDatabase(1);
        //连接池设置
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //最大连接数
        jedisPoolConfig.setMaxTotal(100);
        //最大空闲数
        jedisPoolConfig.setMaxIdle(100);
        //最小空闲连接数
        jedisPoolConfig.setMinIdle(10);
        //最长等待时间
        jedisPoolConfig.setMaxWaitMillis(2000);
        JedisClientConfiguration clientConfiguration = JedisClientConfiguration.builder()
                .usePooling()
                .poolConfig(jedisPoolConfig)
                .build();
        //连接超时2s,读取超时2s,等待超时
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config, clientConfiguration);
        return jedisConnectionFactory;
    }*/

    @Bean
    //@ConditionalOnMissingBean(name = "redisTemplate")// 我们可以自定义一个redisTemplate来替换这个默认的
    //@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        template.setDefaultSerializer(jsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(jsonRedisSerializer);
        return template;
    }

    @Bean
    //@ConditionalOnMissingBean // String类型是最常用的，所以单独建了一个
    //@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
